/**
 * Created by arvin on 2017/4/12.
 */
/**
 这个是保留接口，方便我处理数据库
 */
var Bearcat = require('bearcat');
var Async = require("async")

module.exports = function () {
    return Bearcat.getBean({
        id: "get-updateMotionInfo",
        func: updateMotionInfo,
        props: [
            {name: "app", "ref": "application"}
        ]
    });
}

function updateMotionInfo() {
    this.app = null;
}

var CheckMotionDevice = function (msg) {
    if(msg.params.motion == undefined)
    {
        console.log("4")
    }
    if(msg.params.motionname == undefined ) {
        console.log("3")
    }
    if(msg.params.moviehouse == undefined )
    {
        console.log("2")
    }
    if(msg.params.movieaddress == undefined)
    {
        console.log("1")
    }
    return false;
}

updateMotionInfo.prototype.handle = function (msg,next) {

    var motionName = msg.params.motionname;
    var motion = msg.params.motion;
    var moviehouse = msg.params.moviehouse ;//== undefined || msg.params.MovieAddress
    var movieAddress = msg.params.movieaddress;
    var contactsnumber = msg.params.contactsnumber;
    var contacts = msg.params.contacts;

    var model = this.app.getComponent("dao").getConnection().model("MotionDevices");

    Async.waterfall(
        [
            function (cb) {
                model.invalidate(motion,function (invalid) {
                    if(invalid)
                        cb();
                    else
                        next(null, {code: 102, msg: ""});
                })
            },
            function () {
                model.UpdateMotionInfo(
                    {
                        MotionDevice:motion,
                        MotionName:motionName,
                        MovieHouse : moviehouse,
                        MovieAddress : movieAddress,
                        ContactsNumber : contactsnumber,
                        Contacts: contacts
                    },
                    function (res) {

                        if ( res)
                            next(null,{code:1,msg:"更新设备成功"})
                        else
                            next(null,{code:101,msg:"更新设备失败"})
                    }

                )
            }
        ]
    );

}